import request from './request'

export interface OrderItem {
  id: number
  product_name: string
  variant_name: string
  unit_price: string
  quantity: number
  subtotal: string
}

export interface OrderStatusHistory {
  id: number
  status: string
  status_display: string
  note: string
  created_at: string
}

export interface Order {
  id: number
  order_number: string
  user_id: number
  user_email: string
  user_name: string
  status: string
  status_display: string
  shipping_full_name: string
  shipping_company: string
  shipping_address_line1: string
  shipping_address_line2: string
  shipping_city: string
  shipping_state: string
  shipping_postal_code: string
  shipping_country: string
  shipping_phone: string
  subtotal_price: string
  shipping_cost: string
  total_price: string
  note: string
  admin_note: string
  tracking_number: string
  items: OrderItem[]
  status_history: OrderStatusHistory[]
  created_at: string
  updated_at: string
}

export interface OrderListParams {
  page?: number
  page_size?: number
  status?: string
  search?: string
  ordering?: string
}

// 获取订单列表
export function getOrderList(params?: OrderListParams) {
  return request<{
    count: number
    next: string | null
    previous: string | null
    results: Order[]
  }>({
    url: '/orders/',
    method: 'get',
    params
  })
}

// 获取订单详情
export function getOrderDetail(id: number) {
  return request<Order>({
    url: `/orders/${id}/`,
    method: 'get'
  })
}

// 更新订单状态
export function updateOrderStatus(id: number, data: { status: string; note?: string }) {
  return request<Order>({
    url: `/orders/${id}/update_status/`,
    method: 'post',
    data
  })
}

// 更新管理员备注
export function updateAdminNote(id: number, data: { admin_note: string; tracking_number?: string }) {
  return request<Order>({
    url: `/orders/${id}/update_admin_note/`,
    method: 'post',
    data
  })
}
